package com.wnxy.pawnbackgroundsystem.controller;


import com.wnxy.pawnbackgroundsystem.commons.JwtUtils;
import com.wnxy.pawnbackgroundsystem.commons.ServiceImplUtil;
import com.wnxy.pawnbackgroundsystem.commons.ServletApiUtil;
import com.wnxy.pawnbackgroundsystem.entity.User;
import com.wnxy.pawnbackgroundsystem.dto.userDto.LoginDto;
import com.wnxy.pawnbackgroundsystem.param.UserParam;
import com.wnxy.pawnbackgroundsystem.response.Result;
import com.wnxy.pawnbackgroundsystem.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-10-14
 */
@RestController
//@RequestMapping("/user")
@CrossOrigin("*")
@Slf4j
public class UserController {

    @Resource
    private IUserService userService;

    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto){
        if (null == loginDto){
            return Result.checkFail("没有登陆条件");
        }
        UserParam userParam = null;
        if (null != loginDto.getAccountLogin()){
            log.info("账号登陆功能：参数= {}", loginDto.getAccountLogin());
            userParam = userService.accountLogin(loginDto.getAccountLogin());
        }else  if (null != loginDto.getPhoneLogin()){
            log.info("手机登陆功能：参数= {}", loginDto.getPhoneLogin());
            userParam = userService.phoneLogin(loginDto.getPhoneLogin());
        }
        String jwt = JwtUtils.createJWT(userParam);
        ServletApiUtil.setResponseHander(JwtUtils.AUTH_TOKEN_NAME,jwt);
        return Result.success(String.class,"登陆成功",jwt);
    }
    @GetMapping("qqlogin")
    public String qqlogin(String code,String state){
        log.info("qq登陆功能: = {},{}",code,state);
        String qqjwt = userService.qqLogin(code);
        return qqjwt;
    }
    @GetMapping("findUserMassage")
    public Result findUserMassage(){
        log.info("获取当前登陆用户信息功能！！！");
        UserParam user = userService.findUser();
        return Result.success(UserParam.class,"获取成功",user);
    }
    @GetMapping("chekePhone")
    public Result chekePhone(String phone){
        log.info("验证手机号功能：参数= {}",phone);
        if (null == phone) {
            return  Result.checkFail("手机号不能为空");
        }
//        if(!ServiceImplUtil.chekePhone(phone)){
//            return  Result.checkFail("手机号格式不正确");
//        }
        User phone1 = userService.findPhone(phone);
        return Result.success("手机号存在");
    }
    @GetMapping("phoneCode")
    public Result phoneCode(String phone){
        log.info("验证手机号功能：参数= {}",phone);
        if (null == phone) {
            return  Result.checkFail("手机号不能为空");
        }
        userService.phoneCode(phone);
        return Result.success("验证码发送成功");
    }
}

